﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    // NOTE: If you change the class name "ContractType" here, you must also update the reference to "ContractType" in App.config.
    public partial class MainService : IContractType 
    {
        public int ContractType_Using(int ContractTypeID)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_ContractType_Using", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@ContractTypeID", ContractTypeID);
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet ContractType_GetAll()
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_ContractType_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public DataSet ContractType_GetByBranchCode( string _BranchCode)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_ContractType_GetByBranchCode", dbConn);
            dbCmd.Parameters.AddWithValue("@ContractType_BranchCode", _BranchCode);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public DataSet ContractType_GetList( out int ItemCount,int currentPage, int pageSize, string _BranchCode)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_ContractType_GetList", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            dbCmd.Parameters.AddWithValue("@BranchCode", _BranchCode);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public ContractTypeInfo ContractType_GetInfo(int ContractTypeID)
        {
            ContractTypeInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_ContractType_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@ContractType_ID", ContractTypeID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new ContractTypeInfo();
                    retVal.ID = Convert.ToInt32(dr["ContractType_ID"]);
                    retVal.Name = dr["ContractType_Name"].ToString();
                    retVal.Code = dr["ContractType_Code"].ToString();
                    retVal.Probation =Convert.ToDecimal(dr["ContractType_ProbationSalary"]);
                    retVal.Time = Convert.ToInt32(dr["ContractType_Time"]);
                    retVal.Insurance = Convert.ToBoolean(dr["ContractType_Insurance"]);
                    retVal.BranchCode = dr["ContractType_BranchCode"].ToString();
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public int ContractType_Insert(ContractTypeInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_ContractType_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@ContractType_Name", eInfo.Name);
            dbCmd.Parameters.AddWithValue("@ContractType_ProbationSalary", eInfo.Probation);
            dbCmd.Parameters.AddWithValue("@ContractType_Time", eInfo.Time);
            dbCmd.Parameters.AddWithValue("@ContractType_Insurance", eInfo.Insurance);
            dbCmd.Parameters.AddWithValue("@ContractType_Code", eInfo.Code);
            dbCmd.Parameters.AddWithValue("@ContractType_BranchCode", eInfo.BranchCode);
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool ContractType_Update(ContractTypeInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_ContractType_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@ContractType_ID", eInfo.ID);
            dbCmd.Parameters.AddWithValue("@ContractType_Name", eInfo.Name);
            dbCmd.Parameters.AddWithValue("@ContractType_ProbationSalary", eInfo.Probation);
            dbCmd.Parameters.AddWithValue("@ContractType_Time", eInfo.Time);
            dbCmd.Parameters.AddWithValue("@ContractType_Insurance", eInfo.Insurance);
            dbCmd.Parameters.AddWithValue("@ContractType_Code", eInfo.Code);
            dbCmd.Parameters.AddWithValue("@ContractType_BranchCode", eInfo.BranchCode);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool ContractType_Delete(int ContractTypeID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_ContractType_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@ContractType_ID", ContractTypeID);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}
